package com.sc.ktapplication.module.activity

import android.content.Intent
import android.os.Bundle
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import com.sc.ktapplication.MyApp
import com.sc.ktapplication.R
import com.sc.ktapplication.common.base.BaseActivity
import com.sc.ktapplication.common.util.ToastUtil
import com.sc.ktapplication.module.service.MsgService
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    private var firstPressedTime:Long = 0

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun showErrorMsg() {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val fragmentManager = supportFragmentManager
        val navHostFragment = fragmentManager.findFragmentById(R.id.fragment) as NavHostFragment?
        val navController = navHostFragment!!.navController
        NavigationUI.setupWithNavController(bottomView, navController)

        startService(Intent(this,MsgService::class.java))
    }

    override fun onSupportNavigateUp(): Boolean {
        return Navigation.findNavController(this, R.id.bottomView).navigateUp()
    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - firstPressedTime < 2000) {
            MyApp.instance!!.exit()
        } else {
            ToastUtil.showShort("再按一次退出应用")
            firstPressedTime = System.currentTimeMillis()
        }
    }

}
